/************************************************************************************************************************************
 *	This is a TESTVERSION (Beta) of neosmart STREAM
 *
 *  THE SOFTWARE AND DOCUMENTATION ARE LICENSED "AS IS", AND NEOSMART DISCLAIMS ANY AND ALL OTHER WARRANTIES, WHETHER EXPRESS OR
 *  IMPLIED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, TO THE
 *  EXTENT AUTHORIZED BY LAW. WITHOUT LIMITATION OF THE FOREGOING, NEOSMART EXPRESSLY DOES NOT WARRANTS THAT THE SOFTWARE WILL MEET
 *  YOUR REQUIREMENTS OR THAT OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR FREE. YOU ASSUME RESPONSIBILITY FOR SELECTING 
 *  THE SOFTWARE TO ACHIEVE YOUR INTENDED RESULTS, AND FOR THE RESULTS OBTAINED FROM YOUR USE OF THE SOFTWARE. YOU SHALL BEAR THE 
 *  ENTIRE RISK AS TO THE QUALITY AND THE PERFORMANCE OF THE SOFTWARE.
 *
 *	@copyright:			neosmart GmbH
 *	@licence:			http://www.neosmart-stream.com/beta-test/licence-agreement/
 *	@documentation:		http://www.neomart-stream.com/
 *	@version:			0.1
 *	
 ************************************************************************************************************************************/

(function($) {
	
	$.fn.neosmartStream = function(options) {
		
		var o = $.extend({}, $.fn.neosmartStream.defaults, options);
		var $this = this;
		o.uid = false;
		o.uat = false;
		o.dev = true;
		o.path = 'neosmart_stream/nss_includes/';
		
		init();
		
		function init(){
						
			if(!o.auto_update) return;
			$.ajax({
				url:		o.path+'ajax.config.php',
				complete:	function(xhr, status){
					var json = $.parseJSON(xhr.responseText);
					o.cache_time = parseInt(json.cache_time);
					o.channel_count = parseInt(json.channel_count);
					if(json.update=='true'){
						updateCache();
					}else{
						if(o.dev) return false;
						trace('set timeout > '+o.cache_time+'s');
						setTimeout(updateCache,o.cache_time*1000);
					}
				}
			});
		}
		
		function updateCache(){
			o.updated_count = 0;
			for(var i=0;i<o.channel_count;i++){
				updateChannel(i);
			}
		}

		function updateChannel(i){
			$.ajax({
				url:		o.path+'ajax.update.channel.php?channel='+i,
				complete:	function(xhr, status){
					var json = $.parseJSON(xhr.responseText);
					updateComplete(json.channel,json.status);
				}
			});
		}
		
		function updateComplete(channel,status){
			o.updated_count++;
			trace('channel:'+channel+',status:'+status);
			if(o.updated_count==o.channel_count){
				trace('Updated all channels!');
				mergeChannels();
			}
		}
		
		function mergeChannels(){
			$.ajax({
				url:		o.path+'ajax.merge.channels.php',
				complete:	function(xhr, status){
					addNewContent(xhr.responseText);
				}
			});
		}
		
		function addNewContent(data){
			if(o.dev) return false;
			//$this.html(data);
			trace('set timeout > '+o.cache_time+'s');
			setTimeout(updateCache,o.cache_time*1000);	
		}
		
		return $this;
	};

	/******************************************************************************************************
	 * Defaults 
	 ******************************************************************************************************/
	 
	$.fn.neosmartStream.defaults = {
		cache_time:		60,
		channel_count:	0,
		updated_count:	0,
		auto_update:	true,
		dev:			false
	};

})(jQuery);

function trace(msg){try{console.log(msg);}catch(e){}}